package com.cskaoyan.service;

import com.cskaoyan.bean.bo.CommentReplyBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.CommentPO;
import com.cskaoyan.bean.po.wxPo.MarketComment;
import com.cskaoyan.bean.po.wxPo.MarketCommentExample;
import com.cskaoyan.bean.vo.CommentVO;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.mapper.CommentMapper;
import com.cskaoyan.mapper.wxMapper.MarketCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author by WANGJun
 * @date 2022/6/6 10:04
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;

    @Override
    public TVO<CommentVO> list(BaseParam baseParam,Integer userId, Integer valueId) {
        List<CommentPO> commentPOS = commentMapper.selectCommentByPage(baseParam,userId,valueId);
        ArrayList<CommentVO> commentVOS = new ArrayList<>();
        Iterator<CommentPO> iterator = commentPOS.iterator();
        while (iterator.hasNext()){
            commentVOS.add(CommentVO.setCommentVO(iterator.next()));
        }
        int total = commentMapper.selectCommentCount(userId,valueId);
        TVO<CommentVO> commentVOTVO = new TVO<>();
        commentVOTVO.setTotal(total);
        commentVOTVO.setPages((int)Math.ceil((total+0.0)/baseParam.getLimit()));
        commentVOTVO.setLimit(baseParam.getLimit());
        commentVOTVO.setPage(baseParam.getPage());
        commentVOTVO.setList(commentVOS);
        return commentVOTVO;
    }

    @Override
    public int delete(int id) {
        int deleteCommentById = commentMapper.deleteCommentById(id);
        return deleteCommentById;
    }

    @Override
    public int reply(CommentReplyBO commentReplyBO) {
        MarketComment marketComment = marketCommentMapper.selectByPrimaryKey(commentReplyBO.getCommentId());
        String adminContent = marketComment.getAdminContent();
        if (adminContent == null || "".equals(adminContent)) {
            commentMapper.addReply(commentReplyBO,new Date());
            return 0;
        }
        return 1;
    }
}
